#include "hardcodedPatternAnimation.h"

#include <spri/data.h>
#include <spri/assets.h>

#include <util/indexMap.h>

#include <foun/foun_math.h>
#include <foun/debug.h>

#include <string.h>

/*
 * Internal Constants
 */

/*
 * Public Static
 */
lct::spri::AnimationAsset* HardcodedPatternAnimation::BuildAsset(lct::foun::Allocator* pAllocator)
{
	static const u32 TRACK_COUNT = 1;

	lct::spri::TrackData* pTrackDataArray = pAllocator->AllocTypeArray<lct::spri::TrackData>(TRACK_COUNT);
	lct::spri::KeyData** pKeyDataArrayArray = pAllocator->AllocTypeArray<lct::spri::KeyData*>(TRACK_COUNT);
	{
		u32 trackIndex = 0;
		{
			static const u32 KEY_COUNT = 5;

			lct::spri::KeyData* pKeyDataArray = pAllocator->AllocTypeArray<lct::spri::KeyData>(KEY_COUNT);
			u32 keyIndex = 0;
			{
				lct::spri::KeyData* pKeyData = pKeyDataArray + keyIndex++;
				pKeyData->frame = 0.0f;
				pKeyData->value = 0.0f;
				pKeyData->slope = 0.0f;
				pKeyData->curveType = lct::spri::CURVE_TYPE_FLAT;
			}
			{
				lct::spri::KeyData* pKeyData = pKeyDataArray + keyIndex++;
				pKeyData->frame = 45.0f;
				pKeyData->value = 1.0f;
				pKeyData->slope = 0.0f;
				pKeyData->curveType = lct::spri::CURVE_TYPE_FLAT;
			}
			{
				lct::spri::KeyData* pKeyData = pKeyDataArray + keyIndex++;
				pKeyData->frame = 50.0f;
				pKeyData->value = 0.0f;
				pKeyData->slope = 0.0f;
				pKeyData->curveType = lct::spri::CURVE_TYPE_FLAT;
			}
			{
				lct::spri::KeyData* pKeyData = pKeyDataArray + keyIndex++;
				pKeyData->frame = 55.0f;
				pKeyData->value = 1.0f;
				pKeyData->slope = 0.0f;
				pKeyData->curveType = lct::spri::CURVE_TYPE_FLAT;
			}
			{
				lct::spri::KeyData* pKeyData = pKeyDataArray + keyIndex++;
				pKeyData->frame = 60.0f;
				pKeyData->value = 0.0f;
				pKeyData->slope = 0.0f;
				pKeyData->curveType = lct::spri::CURVE_TYPE_FLAT;
			}
			LCT_ASSERT(keyIndex == KEY_COUNT);

			lct::spri::TrackData* pTrackData = pTrackDataArray + trackIndex;
			strcpy(pTrackData->targetName, "shape_outer");
			pTrackData->targetType = lct::spri::TARGET_TYPE_PATTERN;
			pTrackData->targetPropertyType = lct::spri::PATTERN_PROPERTY_TYPE_TEXTURE_INDEX;
			pTrackData->finishType = lct::spri::FINISH_TYPE_LOOP;
			pTrackData->keyCount = KEY_COUNT;

			pKeyDataArrayArray[trackIndex] = pKeyDataArray;

			++trackIndex;
		}
		LCT_ASSERT(trackIndex == TRACK_COUNT);
	}

	lct::spri::AnimationAsset::Track* pTrackArray = pAllocator->AllocTypeArray<lct::spri::AnimationAsset::Track>(TRACK_COUNT);
	for (u32 trackIndex = 0; trackIndex < TRACK_COUNT; ++trackIndex) {
		lct::spri::AnimationAsset::Track* pTrack = pTrackArray + trackIndex;
		lct::spri::TrackData* pTrackData = pTrackDataArray + trackIndex;
		lct::spri::KeyData* pKeyDataArray = pKeyDataArrayArray[trackIndex];
		pTrack->pTrackData = pTrackData;
		pTrack->pKeyDataArray = pKeyDataArray;
	}

	lct::spri::AnimationData* pAnimationData = pAllocator->AllocType<lct::spri::AnimationData>();
	strcpy(pAnimationData->name, "pattern");
	pAnimationData->trackCount = TRACK_COUNT;

	lct::spri::AnimationAsset* pAnimationAsset = pAllocator->AllocType<lct::spri::AnimationAsset>();
	pAnimationAsset->pAnimationData = pAnimationData;
	pAnimationAsset->pTrackArray = pTrackArray;

	return pAnimationAsset;
}
